#include "StdAfx.h"
#include "CVideoPlayStatu.h"
#include "CStaticText.h"
#include "CGraphicMgr.h"


void CALLBACK UpdateUITimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{
	CVideoPlayStatu* pVideoPlaying = (CVideoPlayStatu*)GRAPHICMGR.GetStatu(L"videoplay");


	if (CEMediaPlayer::GetPlayerInstance()->IsEndOfMedia())
		pVideoPlaying->PlayNext();
	else
	pVideoPlaying->UpdatePlayInfo( );

}

long CVideoPlayStatu::m_procOld = 0;
LRESULT CALLBACK VideoPlayProc(HWND hwnd,  UINT uMsg,  WPARAM wParam, LPARAM lParam )
{
	switch (uMsg)
	{
	/*case  WM_LBUTTONDOWN:
		if (CEMediaPlayer::GetPlayerInstance()->IsFullScreen())
		{
			return 0;
		}
	case WM_LBUTTONUP:
		if (CEMediaPlayer::GetPlayerInstance()->IsFullScreen())
		{
			return 0;
		}
		break;*/
	case WM_LBUTTONDBLCLK:
		{
          //	CVideoPlayStatu* pStatu = (CVideoPlayStatu*)GRAPHICMGR.GetStatu(L"videoplay");
			POINT pt;
		    pt.x = LOWORD(lParam);
			pt.y = HIWORD(lParam);
			RECT rc=CEMediaPlayer::GetPlayerInstance()->GetScreenRect();
			if ((!CEMediaPlayer::GetPlayerInstance()->IsFullScreen())&&PtInRect(&rc, pt))
			{
		
				CEMediaPlayer::GetPlayerInstance()->SetFullScreen(true);
				HDC hScreenDC=::GetDC(hwnd);
				HBRUSH hBrush = CreateSolidBrush(RGB(16,0,16));
				RECT rct = {0,0,SCREEN_WIDTH,SCREEN_HEIGHT};

				FillRect(hScreenDC,&rct,hBrush);
				ReleaseDC(hwnd,hScreenDC);
			   if (hBrush)
				DeleteObject(hBrush);
			    GRAPHICMGR.AllElementShow(false);
			   /*if(hScreenDC)
			   DeleteDC(hScreenDC);*/
			 /*  GRAPHICMGR.AllElementShow(FALSE);
			   GRAPHICMGR.Refresh();
			   	KillTimer(GRAPHICMGR.GetWnd(),CHECK_TIMER);*/
			}
			else
			{
				 if (CEMediaPlayer::GetPlayerInstance()->IsFullScreen())
				 {
					 
					
					 GRAPHICMGR.AllElementShow(TRUE);
					 GRAPHICMGR.AllElementUpdate();
					GRAPHICMGR.Refresh();
					 CEMediaPlayer::GetPlayerInstance()->SetFullScreen(false);
					 
				 }
			}
			
		}
		break;
		
	}
	if (CVideoPlayStatu::m_procOld)
		return CallWindowProc((WNDPROC)(CVideoPlayStatu::m_procOld),hwnd,uMsg,wParam,lParam);
}
CVideoPlayStatu::CVideoPlayStatu(WCHAR* name):IStatu(name)
{

}

bool CVideoPlayStatu::EvaluateClickEvent(const WCHAR* elementName)
{
	

	
	if (wcscmp(elementName,L"video_btn_home") == 0)

		return BackClick();
	else if(wcscmp(elementName,L"video_btn_play") == 0)
         return PlayPause();
	else if(wcscmp(elementName,L"video_btn_pause") == 0)
         return PlayPause();
	else if(wcscmp(elementName,L"video_btn_openlist") == 0)
            return OpenFileList();
	else if(wcscmp(elementName,L"video_btn_pre") == 0)
           return PlayPre();
	else if(wcscmp(elementName,L"video_btn_next") == 0)
           return  PlayNext();
	else if(wcscmp(elementName,L"video_btn_stop") == 0)
           Stop();
	  return true;
}
bool CVideoPlayStatu::Stop()
{
	CEMediaPlayer::GetPlayerInstance()->Stop();

	CStaticText* ptxtplaytime = (CStaticText*)GRAPHICMGR.GetElement(L"txtplaytime");
	CStaticText* ptxttotaltime = (CStaticText*)GRAPHICMGR.GetElement(L"txttotaltime");
	CStaticText* ptxtsongtitle = (CStaticText*)GRAPHICMGR.GetElement(L"txtsongtitle");



	if (ptxtsongtitle)
	{
		wstring strTitle=CEMediaPlayer::GetPlayerInstance()->GetTitle();
		ptxtsongtitle->setvalue(strTitle.c_str());
	}


	if (ptxtplaytime)
		ptxtplaytime->setvalue(L"00:00");
	if (ptxttotaltime)
		ptxttotaltime->setvalue(L"00:00");


	int iPlayPercent=CEMediaPlayer::GetPlayerInstance()->GetPlayPercent(100);
	CSlider *pSilder=(CSlider*)GRAPHICMGR.GetElement(L"video_slider");
	pSilder->SetRange(0,100);
	pSilder->SetPos(0);
	CButton * pBtn1 = (CButton*)GRAPHICMGR.GetElement(L"video_btn_play");
	if (pBtn1)
		pBtn1->m_bVisible = true;
	CButton * pBtn = (CButton*)GRAPHICMGR.GetElement(L"video_btn_pause");
	if (pBtn)
		pBtn->m_bVisible = false;


	GRAPHICMGR.SetUpdateElements(L"txtplaytime",L"txttotaltime",L"video_slider",L"txtsongtitle",L"video_btn_pause",L"none");

    KillTimer(GRAPHICMGR.GetWnd(),CHECK_TIMER);
	return true;
}
bool CVideoPlayStatu::EvaluateSliderMoveEvent(const WCHAR* elementName)
{
	

	if (wcscmp(elementName,L"video_slider") == 0)
		return SeekSliderClick();
	return true;
}


bool CVideoPlayStatu::EnterStatu()
{


	CStaticText* ptxtplaytime = (CStaticText*)GRAPHICMGR.GetElement(L"txtplaytime");
	CStaticText* ptxttotaltime = (CStaticText*)GRAPHICMGR.GetElement(L"txttotaltime");
	CStaticText* ptxtsongtitle = (CStaticText*)GRAPHICMGR.GetElement(L"txtsongtitle");



	if (ptxtsongtitle)
	{
		wstring strTitle=CEMediaPlayer::GetPlayerInstance()->GetTitle();
		ptxtsongtitle->setvalue(strTitle.c_str());
	}


	if (ptxtplaytime)
		ptxtplaytime->setvalue(L"00:00");
	if (ptxttotaltime)
		ptxttotaltime->setvalue((CEMediaPlayer::GetPlayerInstance()->GetTotalPlayTime()).c_str());

	/*CEMediaPlayer::GetPlayerInstance()->SetVideoVolume(100);
	CSlider *pSilder=(CSlider*)GRAPHICMGR.GetElement(L"video_slider");
	pSilder->SetRange(1,9);

	pSilder->SetPos(GlobalStatic::GetVolume());*/

	/*if (CEMediaPlayer::GetPlayerInstance()->GetVideoState())
	{
		SetTimer(GRAPHICMGR.GetWnd(),CHECK_TIMER,300,UpdateUITimerProc);
	}*/

   m_procOld = SetWindowLong(GRAPHICMGR.GetWnd(),GWL_WNDPROC,(long)VideoPlayProc);
	return TRUE;
}
bool CVideoPlayStatu::ExitStatu()
{
	KillTimer(GRAPHICMGR.GetWnd(),CHECK_TIMER);
	SetWindowLong(GRAPHICMGR.GetWnd(),GWL_WNDPROC,m_procOld);
	return true;
}

void CVideoPlayStatu::StartPlay( )
{
	int nCount = 0;
	
	CListBox* pList = (CListBox*)GRAPHICMGR.GetElement(L"listview_video");
	nCount=CEMediaPlayer::GetPlayerInstance()->GetListCount();

	if(nCount>=0)
	{
        
		wstring strDir=pList->GetCurDir();
		int a=pList->GetListTotalItem()-pList->GetListTotalFile();
		if(nCount != pList->GetListTotalItem()-pList->GetListTotalFile())
		{
			m_strDir=strDir;
			CEMediaPlayer::GetPlayerInstance()->ClearPlayList();
			for(int i = 0; i < pList->GetListTotalItem()-pList->GetListTotalFile(); i++)
			{
				CEMediaPlayer::GetPlayerInstance()->AddFile(pList->GetListItemData(i+pList->GetListTotalFile()));
			}
			//pList->SetScrollBar();
		}
	}
	wstring  strPlayFile=pList->GetItemData(pList->GetListSelectedItem());
  
	CEMediaPlayer::GetPlayerInstance()->SetPlayIndex( pList->GetListSelectedItem()- pList->GetTotalFile());
	//CEMediaPlayer::GetPlayerInstance()->Play();
	//UpdatePlayInfo(  );
	CButton * pBtn1 = (CButton*)GRAPHICMGR.GetElement(L"video_btn_play");
	if (pBtn1)
		pBtn1->m_bVisible = false;
	CButton * pBtn = (CButton*)GRAPHICMGR.GetElement(L"video_btn_pause");
	if (pBtn)
		pBtn->m_bVisible = true;
	SetTimer(GRAPHICMGR.GetWnd(),CHECK_TIMER,300,UpdateUITimerProc);
}

bool CVideoPlayStatu::BackClick()
{
	CEMediaPlayer::GetPlayerInstance()->Stop();

	::PostMessage(GRAPHICMGR.GetWnd(),WM_DESTROY,0,0);
	return false;
}

void CVideoPlayStatu::UpdatePlayInfo()
{
	if (CEMediaPlayer::GetPlayerInstance()->IsFullScreen())
	{
		return ;
	}
	CStaticText* ptxtsongtitle = (CStaticText*)GRAPHICMGR.GetElement(L"txtsongtitle");
	if (ptxtsongtitle)
	{
		ptxtsongtitle->setvalue((CEMediaPlayer::GetPlayerInstance()->GetTitle()).c_str());
	}

	CStaticText* ptxttotaltime = (CStaticText*)GRAPHICMGR.GetElement(L"txttotaltime");
	if (ptxttotaltime)
	{
		ptxttotaltime->setvalue((CEMediaPlayer::GetPlayerInstance()->GetTotalPlayTime()).c_str());
	}
	wstring strplaytime=	CEMediaPlayer::GetPlayerInstance()->GetCurPlayTime();
	CStaticText* ptxtplaytime = (CStaticText*)GRAPHICMGR.GetElement(L"txtplaytime");
	ptxtplaytime->setvalue(strplaytime.c_str());

	int iPlayPercent=CEMediaPlayer::GetPlayerInstance()->GetPlayPercent(100);
	CSlider *pSilder=(CSlider*)GRAPHICMGR.GetElement(L"video_slider");
	pSilder->SetRange(0,100);
	pSilder->SetPos(iPlayPercent);
	
	GRAPHICMGR.SetUpdateElements(L"txtplaytime",L"txttotaltime",L"video_slider",L"txtsongtitle",L"none");

}
bool  CVideoPlayStatu::SeekSliderClick()
{
	CSlider *pSilder=(CSlider*)GRAPHICMGR.GetElement(L"video_slider");
	int fRat=pSilder->GetPos();
	CEMediaPlayer::GetPlayerInstance()->SetPlayPercent(fRat,100);
	return true;
}


bool CVideoPlayStatu::OpenFileList()
{
	if( CEMediaPlayer::GetPlayerInstance()->IsRunning() )
	{
		if(CEMediaPlayer::GetPlayerInstance()->Pause())
		{

			CButton * pBtn1 = (CButton*)GRAPHICMGR.GetElement(L"video_btn_play");
			if (pBtn1)
				pBtn1->m_bVisible = false;
			CButton * pBtn = (CButton*)GRAPHICMGR.GetElement(L"video_btn_pause");
			if (pBtn)
				pBtn->m_bVisible = true;
			GRAPHICMGR.SetUpdateElements(L"video_btn_play",L"none");
			KillTimer(GRAPHICMGR.GetWnd(),CHECK_TIMER);
		}
	}
	else
	{

	}

    CListBox* pList = (CListBox*)GRAPHICMGR.GetElement(L"listview_video");
	int a=pList->GetListTotalFile();
    pList->SetListSelectedItem(a+CEMediaPlayer::GetPlayerInstance()->GetPlayIndex());

	GRAPHICMGR.PostChange(L"videolist");
	return false;
}

bool CVideoPlayStatu::PlayPause()
{
	
	
	
	
	if( CEMediaPlayer::GetPlayerInstance()->IsRunning() )
	{
		if(CEMediaPlayer::GetPlayerInstance()->Pause())
		{

			CButton * pBtn1 = (CButton*)GRAPHICMGR.GetElement(L"video_btn_play");
			if (pBtn1)
				pBtn1->m_bVisible = true;
			CButton * pBtn = (CButton*)GRAPHICMGR.GetElement(L"video_btn_pause");
			if (pBtn)
				pBtn->m_bVisible = false;
			GRAPHICMGR.SetUpdateElements(L"video_btn_pause",L"none");
			KillTimer(GRAPHICMGR.GetWnd(),CHECK_TIMER);
		}
	}
	else
	{
		if (CEMediaPlayer::GetPlayerInstance()->GetPlayStatus()==PLAY_STATUS_STOP)
		{
			CEMediaPlayer::GetPlayerInstance()->SetPlayIndex(CEMediaPlayer::GetPlayerInstance()->GetPlayIndex());
			
			//SetTimer(GRAPHICMGR.GetWnd(),CHECK_TIMER,300,UpdateUITimerProc);
			
			//return  TRUE;
			CButton * pBtn1 = (CButton*)GRAPHICMGR.GetElement(L"video_btn_play");
			if (pBtn1)
				pBtn1->m_bVisible = false;
			CButton * pBtn = (CButton*)GRAPHICMGR.GetElement(L"video_btn_pause");
			if (pBtn)
				pBtn->m_bVisible = true;
			SetTimer(GRAPHICMGR.GetWnd(),CHECK_TIMER,300,UpdateUITimerProc);
			GRAPHICMGR.SetUpdateElements(L"video_btn_play",L"none");
			return true;
		}
		
		if(CEMediaPlayer::GetPlayerInstance()->Play())
		{
			CButton * pBtn1 = (CButton*)GRAPHICMGR.GetElement(L"video_btn_play");
			if (pBtn1)
				pBtn1->m_bVisible = false;
			CButton * pBtn = (CButton*)GRAPHICMGR.GetElement(L"video_btn_pause");
			if (pBtn)
				pBtn->m_bVisible = true;
			SetTimer(GRAPHICMGR.GetWnd(),CHECK_TIMER,300,UpdateUITimerProc);
			GRAPHICMGR.SetUpdateElements(L"video_btn_play",L"none");
		}
	}
	return true;
}

bool CVideoPlayStatu::PlayPre()
{
	CEMediaPlayer::GetPlayerInstance()->PlayPre();
	if (CEMediaPlayer::GetPlayerInstance()->IsFullScreen())
	{
		return  false;
	}
	GRAPHICMGR.SetUpdateElements(L"txtsongtitle",L"txtlyric",L"txtplaytime",L"txttotaltime",L"none");

	return true;
}

bool CVideoPlayStatu::PlayNext()
{
	CEMediaPlayer::GetPlayerInstance()->PlayNext();
	if (CEMediaPlayer::GetPlayerInstance()->IsFullScreen())
	{
		return false;
	}
	GRAPHICMGR.SetUpdateElements(L"txtsongtitle",L"txtlyric",L"txtplaytime",L"txttotaltime",L"none");

	return true;
}